package com.simple.datax.dao.impl;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.datax.dao.IDataxFlowTemplateDAO;
import com.simple.datax.vo.DataxFlowTemplateVO;
import com.simple.util.common.StringUtil;
import com.simple.util.object.PageInfoBean;

@Repository("dataxFlowTemplateDAO")
public class DataxFlowTemplateDAOImpl extends BaseDAO implements
		IDataxFlowTemplateDAO {

	@Override
	public DataxFlowTemplateVO findById(String id) {
		return super.get(DataxFlowTemplateVO.class, id);
	}

	@Override
	public void saveOrUpdate(DataxFlowTemplateVO dataxFlowTemplateVO) {
		super.saveOrUpdate(dataxFlowTemplateVO);
	}

	@Override
	public void update(DataxFlowTemplateVO dataxFlowTemplateVO) {
		super.saveOrUpdate(dataxFlowTemplateVO);
	}

	@Override
	public List<DataxFlowTemplateVO> findAll() {
		return super.query(DataxFlowTemplateVO.class);
	}

	@Override
	public void delete(DataxFlowTemplateVO dataxFlowTemplateVO) {
		super.delete(dataxFlowTemplateVO);
	}

	@Override
	public void delete(String id) {
		super.delete(DataxFlowTemplateVO.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DataxFlowTemplateVO> findListByPage(String flowTemplateName,String flowTemplateId,
			String businessType,String creatTimeStart,String creatTimeEnd,String creater,String status,PageInfoBean pageInfoBean){
		final Map<String, Object> paramMap = new LinkedHashMap<String,Object>();
		final StringBuffer sql = new StringBuffer(400);
		
		sql.append( "select  d from DataxFlowTemplateVO d where 1>0 " );
        if(!StringUtil.isNull(flowTemplateName)){
        	sql.append( " and d.flowTemplateName like :flowTemplateName" );
            paramMap.put("flowTemplateName", "%"+flowTemplateName+"%");
        }
        if(!StringUtil.isNull(flowTemplateId)){
        	sql.append( " and d.flowTemplateId like :flowTemplateId" );
            paramMap.put("flowTemplateId", "%"+flowTemplateId+"%");
        }
        if(!StringUtil.isNull(businessType)){
        	sql.append( " and d.businessType=:businessType" );
            paramMap.put("businessType", businessType);
        }
        if(!StringUtil.isNull(status)){
        	sql.append( " and d.status=:status" );
            paramMap.put("status", new BigDecimal(status));
        }
        if(!StringUtil.isNull(creater)){
        	sql.append( " and d.creater like :creater" );
            paramMap.put("creater", "%"+creater+"%");
        }
        if(!StringUtil.isNull(creatTimeStart)){
        	sql.append( " and to_char(d.creatTime,'YYYY-MM-DD')>=:creatTimeStart" );
            paramMap.put("creatTimeStart", creatTimeStart);
        }
        if(!StringUtil.isNull(creatTimeEnd)){
        	sql.append( " and to_char(d.creatTime,'YYYY-MM-DD')<=:creatTimeEnd" );
            paramMap.put("creatTimeEnd", creatTimeEnd);
        }
        sql.append( " order by d.flowTemplateId desc" );
        
		return (List<DataxFlowTemplateVO>)this.queryListByPage("d.flowTemplateId", sql.toString(), paramMap, pageInfoBean);
	}
	
	@Override
	public List<DataxFlowTemplateVO> findAllActive() {
		String hql="from DataxFlowTemplateVO d where 1>0 and d.status=?  order by d.flowTemplateName desc";
		return super.query(hql,BigDecimal.valueOf(2));
	}
	@Override
	public String generateId() {
		List<?> list =  getSession().createSQLQuery("select seq_flow_template_id.nextval from dual").list();
		return (list.get(0)).toString();
	}

}
